<?php
/**
 *
 * 输入数组，输出组装好的feed HTML数组
 * 注：除了content字段，其他字段都是没经过格式化和过虑的，在这里统一过滤
 *    1，统一你数组的键值
 *    2，把res数组放进来(因为来源不同，实在无法统一去取)
 *    3，格式化好你的content字段(包括quote的content)
 *
 * @param array $feed_data
 * quote单条记录规则
 * array(
 *        'uid' =>
 *     'feed_rid' =>
 *        'content' => //这个字段样式很多，请自己拼装好
 *        'from' =>    //1,或2
 *     'show_type'=>//分为tex，quote，pic三种类型
 *        'ctime' =>    // yyyy-mm-dd HH:ii:ss
 *     'rid'   =>   //宝贝rid（quote）
 *     'res'   =>array('uid'=>,'content'=>,'pic_id'=>,'gd_price'=>,'gd_title'=>,'type_xrank'=>)//content表或者
 *               array('uid'=>,'content'=>)//userfeed表
 *        'show_share' => false/true  //是否需要显示分享到微博
 *     'show_del'   => false/true  //是否需要显示删除feed
 *     'show_com'   => false/true  //是否需要显示回复feed
 *     'show_com_cnt'   => false/true  //是否需要显示回复feed
 * )
 * pic单条记录规则
 * array(
 *        'uid' =>
 *     'feed_rid' =>
 *        'content' => //这个字段样式很多，请自己拼装好
 *        'from' =>    //1,或2
 *     'show_type'=>//分为tex，quote，pic三种类型
 *        'ctime' =>    // yyyy-mm-dd HH:ii:ss
 *     'rid'   =>   //宝贝rid
 *     'res'  => array('pic_id'=>,'gd_price'=>,'gd_title'=>,'type_xrank'=>)
 *        'show_share' => false/true  //是否需要显示分享到微博
 *     'show_del'   => false/true  //是否需要显示删除feed
 *     'show_com'   => false/true  //是否需要显示回复feed
 *     'show_com_cnt'   => false/true  //是否需要显示回复feed
 * )
 * txt单条记录规则
 * array(
 *        'uid' =>
 *     'feed_rid' =>
 *        'content' => //这个字段样式很多，请自己拼装好
 *        'from' =>    //1,或2
 *     'show_type'=>//分为tex，quote，pic三种类型
 *        'ctime' =>    // yyyy-mm-dd HH:ii:ss
 *        'show_share' => false/true  //是否需要显示分享到微博
 *     'show_del'   => false/true  //是否需要显示删除feed
 *     'show_com'   => false/true  //是否需要显示回复feed
 *     'show_com_cnt'   => false/true  //是否需要显示回复feed
 * )
 * @return unknown
 */
class ml_biz_feed_abstract{

    private $data;
    private $uids = array();
    protected $nicks = array();
    private $cnt_rids = array();
    private $pic_rids = array();//pic的宝贝rid用来获得cnt不获得content
    private $cnt_comment = array();//feed的回复数和宝贝的评论数（如果有的话）
    private $rids = array();
    private $oContent;
    private $contents = array();
    private $rdsCnt;
    private $Cnts = array();
    private $ifilike;

    public function __construct(){

        $this->rdsCnt=new ml_model_rdsCount();

    }

    //核心逻辑
    public function parse($data){

        $this->data = $data;
        $this->get_rid_uid();
        $this->getNick();//先获得nick再formatuser因为下面会用到昵称
        $this->_format_user();
        $this->get_cnt();
        $this->get_ifilike();
        return $this->_over();
    }

    protected function get_rid_uid(){

        foreach ($this->data as $value) {

            $this->uids[] = $value['res']['uid'];
            if(!empty($value['res']['rid'])){
                $this->cnt_rids[] = $value['res']['rid'];
            }
            if(!empty($value['feed_rid'])){
                $this->cnt_rids[] = $value['feed_rid'];
            }
            $this->uids[] = $value['uid'];

        }
        $this->cnt_rids = array_unique($this->cnt_rids);
        $this->uids = array_unique($this->uids);
    }
    public function get_ifilike(){
        $session = new Lib_memSession("SessionID");
        $visitor=$session->getVal('MEILA_LOGIN');
        if($visitor['uid']){
            $olike=new ml_model_dbILike();
            $olike->isIAttByRids($visitor['uid'],$this->cnt_rids);
            $this->ifilike =$olike->get_data();
        }else{
            $this->ifilike=array();
        }
    }
    protected  function get_cnt()
    {
        if(!empty($this->cnt_rids)){
            //获取评论数
            $rs = $this->rdsCnt->getCommentCount($this->cnt_rids);
            $this->cnt_comment = $this->rdsCnt->get_data();
            //获取喜欢数
            $rs = $this->rdsCnt->getAttitudeCount($this->cnt_rids);
            $this->cnt_like = $this->rdsCnt->get_data();
        }

    }
    

    protected function _format_user($uid)
    {
        return  array(
                'uid' => $uid,
                'nick' => Tool_string::un_html($this->nicks[$uid]),
                'homeurl' => ml_tool_urlMaker::person_index($uid),
                'portrait' => ml_tool_picid::uid2portrait($uid)
        );
    }

    protected function getNick()
    {
        $this->nicks = ml_tool_nick::uids2nicks($this->uids);

    }

    private function _over(){

        $html = array();
        foreach ($this->data as $v) {
            if($v['show_del']&&$v['show_com']){
                $v['first_segment'] = true;
            }
            if($v['show_com']&&$v['show_share']){
                $v['second_segment'] = true;
            }
            $v['ctime'] = ml_tool_standardShow::time($v['ctime']);
            $v['user']  = $this->_format_user($v['uid']);
            $v['nick'] = Tool_string::un_html($this->nicks['uid']);
            if(empty($v['from']))$v['from'] = ML_FROM_WEB;//默认网站
            $v['from'] = $v['from']==ML_FROM_WEB?"来自美啦网站":"来自美啦客户端";
            $v['cnt'] = empty($this->cnt_comment[$v['feed_rid']])?'':'('.$this->cnt_comment[$v['feed_rid']].')';
            
            
            switch ($v['show_type'])
            {

                case 'pic':
                    $v['res']['user'] = $this->_format_user($v['res']['uid']);
                    $v['res']['cnt'] = empty($this->cnt_comment[$v['rid']])?0:$this->cnt_comment[$v['rid']];
                    $v['res']['content'] = ml_tool_formatContent::format_content($v['res']['content']);
                    $v['res']['goods_url'] = ml_tool_urlMaker::show_goods($v['rid']);
                    $v['res']['picurl']=ml_tool_picid::pid2url($v['res']['pic_id'],ML_USER_PICSIZE_FEED,ML_PICTYPE_CONTENT);
                    $v['res']['picurl_big']=ml_tool_picid::pid2url($v['res']['pic_id'],ML_USER_PICSIZE_SHOW,ML_PICTYPE_CONTENT);
                    $buy_url = empty($v['res']['gd_info']['gd_click_url']) ? $v['res']['gd_info']['gd_url']:$v['res']['gd_info']['gd_click_url'];
                    $v['res']['buy_url'] =empty($v['res']['gd_info']['gd_click_tinyurl'])? $buy_url:ml_tool_urlMaker::tinyurl($v['res']['gd_info']['gd_click_tinyurl']);
                    $v['res']['shop_cn']=ml_tool_shop::url2shopcn($v['res']['gd_info']['gd_url']);
                    $v['res']['shop_logo'] =ml_tool_shop::url2shoplogo($v['res']['gd_info']['gd_url']);                                $v['res']['like_cnt'] = empty($this->cnt_like[$v['rid']])?'0':$this->cnt_like[$v['rid']];
                    $v['res']['ifilike'] = $this->ifilike[$v['rid']];

                    $v['is_pic'] = $v['res']['type_xrank']==ML_CONTENT_XRANK_IMG?true:false;
                    $html[] = ml_tool_template::parseModTpl($v , 'pub_feed::txtpic');break;

                case 'txt':
                    $html[] = ml_tool_template::parseModTpl($v , 'pub_feed::txtonly');break;
                case 'quote':
                    if (!empty($v['res'])){
                        $v['is_del'] = false;
                        if(empty($v['res']['pic_id'])){//commentMe
                            $v['res']['goods_url'] = ml_tool_urlMaker::person_feed($v['res']['uid']);//feed的链接
                            $v['res']['user'] = $this->_format_user($v['res']['uid']);
                            $v['res']['cnt'] = empty($this->cnt_comment[$v['rid']])?0:$this->cnt_comment[$v['rid']];
                        }
                        else{
                            $v['res']['user'] = $this->_format_user($v['res']['uid']);
                            $v['res']['cnt'] = empty($this->cnt_comment[$v['rid']])?0:$this->cnt_comment[$v['rid']];
                            $v['res']['like_cnt'] = empty($this->cnt_like[$v['rid']])?'0':$this->cnt_like[$v['rid']];
                            $v['res']['content'] = ml_tool_formatContent::format_content($v['res']['content']);
                            $v['res']['picurl']=ml_tool_picid::pid2url($v['res']['pic_id'],ML_USER_PICSIZE_FEED,ML_PICTYPE_CONTENT);
                            $v['res']['picurl_big']=ml_tool_picid::pid2url($v['res']['pic_id'],ML_USER_PICSIZE_SHOW,ML_PICTYPE_CONTENT);
                            $v['res']['gd_title'] = Tool_string::un_html($v['res']['gd_title']);
                            $v['res']['goods_url'] = ml_tool_urlMaker::show_goods($v['rid']);
                            $v['is_pic'] = $v['res']['type_xrank']==ML_CONTENT_XRANK_IMG?true:false;
                            $buy_url = empty($v['res']['gd_info']['gd_click_url']) ? $v['res']['gd_info']['gd_url']:$v['res']['gd_info']['gd_click_url'];
                            $v['res']['buy_url'] =empty($v['res']['gd_info']['gd_click_tinyurl'])? $buy_url:ml_tool_urlMaker::tinyurl($v['res']['gd_info']['gd_click_tinyurl']);
                            $v['res']['shop_cn']=ml_tool_shop::url2shopcn($v['res']['gd_info']['gd_url']);
                            $v['res']['shop_logo'] =ml_tool_shop::url2shoplogo($v['res']['gd_info']['gd_url']);                
                            $v['res']['ifilike'] =$this->ifilike[$v['rid']];
                        }
                    }
                    else{
                        $v['is_del'] = true;
                    }
                    $html[] = ml_tool_template::parseModTpl($v , 'pub_feed::txtquote');break;
            }
            $home[]=$v;
        }
        return $html;
    }

    /**
     *根据表态生成title文案
     *
     */
    protected  function makeTitle($att){
        $rs='';
        switch ($att) {
            case '0':
                $rs="可以有";
                break;
            case '1':
                $rs="长草了";
                break;
            case '2':
                $rs="不感冒";
                break;
            case '3':
                $rs="喜欢";
                break;

        }
        return $rs;
    }
    protected  function format_public($content){
        return ml_tool_formatContent::format_content($content);
    }

    protected function formate_att_content($att){
        $attitude = $att+6;
        return "<img class='ico_pinbo ico_pb0".$attitude."' title='".$this->makeTitle($att)."' src='".ML_RIA_IMG."common/transparent.gif'  evaluate='0'>".$this->makeTitle($att);
    }
    protected function formate_tag_content($content){

        $str = '';
        foreach (explode(',',$content) as $value) {
            $str.='"<a href="javascript:;" class="ml_linka">'.$value.'</a>" ';
        }
        $str="我刚刚添加了我的标签：".$str."。";
        return $str;
    }

    protected function formate_dec_content($content){
        return "修改美啦签名为：".$content;
    }
    protected function format_collect_content($val){
        $url=ml_tool_urlMaker::col_content($val['feed_info']['col_id'],$val['uid']);
        return $val['content'].'--收集到<a target="_blank" class="ml_linka" href="'.$url.'">【'.$val['feed_info']['col_name'].'】</a>专辑中';

    }
    //喜欢中间人的格式化
    protected function format_like_user($uid,$nick){
        
        return ' // <a class="ml_linka" alt="'.$nick.'" href="'.ml_tool_urlMaker::person_index($uid).'" uid="'.$uid.'">@'.$nick.' :</a>'; 
        
    }

}
?>